package yzy.test.web.servlet;

import yzy.test.web.handler.HandlerManager;
import yzy.test.web.handler.MappingHandler;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class DispatcherServlet implements Servlet {
    @Override
    public void init(ServletConfig config) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        //通过类扫描加载所有使用 @Controller注解包含的类，通过URI调用对应Controller的具体方法
        for(MappingHandler mappingHandler : HandlerManager.mappingHandlerList){
            if(mappingHandler.handle(req, res)){
                return;
            }else{
                System.out.println("can not find Mapping Controller...");
            }
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
